package com.sheep.api.hive.dao.impl;

import com.sheep.api.hive.dao.HiveCommonDao;
import com.sheep.api.hive.dao.LocalHiveClientFactory;
import org.springframework.data.hadoop.hive.HiveClient;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 16-4-25
 * Time: 下午3:02
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class HiveCommonByFactoryDaoImpl extends LocalHiveClientFactory implements HiveCommonDao {
    @Override
    public void createTable() {
    }

    @Override
    public List<String> getAllTableRecord(String tableName) {
        HiveClient hiveClient = getHiveClient();
        try {
            return hiveClient.execute("select * from " + tableName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                hiveClient.shutdown();
            } catch (SQLException tex) {
            }
        }
        return null;
    }


    @Override
    public List<String> getTableRecord(String key, String tableName) {
        HiveClient hiveClient = getHiveClient();
        try {
            return hiveClient.execute("select * from " + tableName + " where anfi = '" + key + "'");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                hiveClient.shutdown();
            } catch (SQLException tex) {
                tex.printStackTrace();
            }
        }
        return null;
    }
}
